package slw.main
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.ui.Mouse;
	
	import slw.core.UIComponent;
	
	/**
	 * Debuger
	 * 调试面板类,用于输出调试信息<br/>
	 * 只需调用一次构造函数，并将调试面板实例添加到显示列表，即可使用静态属性访问单例调用调试方法，同时调用trace()方法在控制台输出，相当于将trace()信息输出到显示列表和控制台:<br/>
	 * <p>
	 * 	<listing>
	 *   this.addChild(new Debuger());<br/>
	 *		
	 *   Debuger.instance.debug(123,458);<br/>
	 * 	</listing>
	 * </p>
	 * 
	 * @author ddx<br/>
	 * 2011-7-4
	 * 
	 * 
	 *
	 */
	public class Debuger extends UIComponent
	{
		//单例
		private static var _instance:Debuger;
		//宽度
		private static var _width:Number=NaN;
		//高度
		private static var _height:Number=NaN;
		
		//上下文菜单项
		private var _menuItem:ContextMenuItem;
		//父容器
		private var _main:DisplayObjectContainer;
		
		/**
		 * 构造函数
		 * @param width:Number=300 Debuger实例宽度
		 * @param height:Number=200 Debuger实例高度
		 * 
		 */		
		public function Debuger(width:Number=300,height:Number=200)
		{
			if(_instance!=null){
				throw new Error("Debuger 为单例模式，构造函数只需调用一次.");
			}else{
				_instance=this;
				
				init();
				
				_width=width;
				_height=height;
			}
		}
		
		/*
		-----------------------------------
		setters getters
		-----------------------------------
		*/
		//
		/**
		 * Debuger 单例
		 * @return Debuger
		 * 
		 */		
		public static function get instance():Debuger{
			if(_instance==null){
				new Debuger();
			}
			return _instance;
		}
		/*
		-----------------------------------
		public  methods
		-----------------------------------
		*/
		//
		/**
		 * 输出调试信息 
		 * @param args: 任意参数
		 * 
		 * 
		 */		
		public function debug(...args):void{//若需修改显示面板显示对象，只需修改此方法，当前只有TextField
			var $debugText:TextField;
			if(this.getChildByName("debugText")==null){
				$debugText=new TextField();
				$debugText.name="debugText";
				$debugText.width=_width;
				$debugText.height=_height;
				$debugText.background=true;
				$debugText.border=true;
				$debugText.type="input";
				$debugText.doubleClickEnabled=true;
				this.addChild($debugText);
			}
			
			var $content:String="";
			for(var i:String in args){
				$content+=args[i]+" ";
			}
			
			$debugText=this.getChildByName("debugText") as TextField;
			$debugText.appendText($content+"\n");
			
			show(true);
			trace(args);
		}
		//
		/**
		 * 显示和隐藏调试面板 
		 * @param visible:Boolean
		 */		
		public function show(visible:Boolean):void{
			if(visible){
				if(_main!=null){
					_main.addChild(this);
					_main.setChildIndex(this,_main.numChildren-1);
				}
			}else{
				if(this.parent!=null){
					_main.removeChild(this);
				}
			}
		}
		//
		/**
		 * 销毁对象实例，并释放资源(从显示列表删除，删除所有侦听)
		 * 
		 */		
		override public function destroy():void{
			if(_main!=null){
				var $index:int=_main.contextMenu.customItems.indexOf(_menuItem);
				_main.contextMenu.customItems.splice($index,1);
			}
			
			_instance=null;
			_width=NaN;
			_height=NaN;
			
			removeListeners();
			super.destroy();
		}
		/*
		-----------------------------------
		private  methods
		-----------------------------------
		*/
		//初始化
		private function init():void{
			this.doubleClickEnabled=true;
			
			addListeners();
		}
		//添加侦听
		private function addListeners():void{
			this.addEventListener(Event.ADDED_TO_STAGE,thisAddedToStage);
			this.addEventListener(MouseEvent.DOUBLE_CLICK,thisDoubleClick);
			this.addEventListener(MouseEvent.MOUSE_DOWN,thisMouseDown);
		}
		//删除侦听
		private function removeListeners():void{
			this.removeEventListener(Event.ADDED_TO_STAGE,thisAddedToStage);
			this.removeEventListener(MouseEvent.DOUBLE_CLICK,thisDoubleClick);
			
			if(_menuItem!=null){
				_menuItem.removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelect);
			}
		}
		//第1次添加到显示列表
		private function thisAddedToStage(e:Event):void{
			_menuItem=new ContextMenuItem(getName()+"...",true);
			_menuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelect);
				
			var $menu:ContextMenu=new ContextMenu();
			$menu.customItems.push(_menuItem);
				
			_main=this.parent as DisplayObjectContainer;
			_main.contextMenu=$menu;
			
			show(false);
			
			this.removeEventListener(Event.ADDED_TO_STAGE,thisAddedToStage);
		}
		//双击隐藏
		private function thisDoubleClick(e:MouseEvent):void{
			show(false);
		}
		//鼠标按下
		private function thisMouseDown(e:MouseEvent):void{
			this.startDrag();
			Mouse.cursor="hand";
			
			if(this.stage!=null){
				this.stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
				this.addEventListener(MouseEvent.MOUSE_MOVE,thisMouseMove);
			}
		}
		//鼠标弹起
		private function stageMouseUp(e:MouseEvent):void{
			this.stopDrag();
			Mouse.cursor="auto";
			
			if(this.stage!=null){
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
				this.removeEventListener(MouseEvent.MOUSE_MOVE,thisMouseMove);
			}
		}
		//鼠标移动
		private function thisMouseMove(e:MouseEvent):void{
			e.updateAfterEvent();
		}
		//单击菜单项
		private function menuItemSelect(e:ContextMenuEvent):void{
			show(true);
		}
		//菜单项名称
		private function getName():String{
			var $name:String=this.toString();
			$name=$name.split(" ")[1];
			$name=$name.slice(0,$name.length-1);
			
			return $name;
		}
	}
}